﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mshtml;

namespace SevenDragon
{
    public partial class stockcode : Form
    {
        System.Uri url;

        int i = 2;//从i页开始获取

        List<temp1> list;
        SecuritiesAnalysisDataContext db;

        //http://stock.finance.qq.com/cgi-bin/sstock/ggcx?zhengquandm=600003

        string prefix = "http://stock.finance.qq.com/cgi-bin/sstock/ggcx?zhengquandm=";

        public stockcode()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void stockcode_Load(object sender, EventArgs e)
        {
            ////浏览器加载成功
            this.url = new Uri("http://stock.finance.qq.com/hqing/hqst/paiminglistsa" + i.ToString() + ".htm");
            this.webBrowser1.Url = url;
            list = new List<temp1>();
            db = new SecuritiesAnalysisDataContext();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
            {
                return;
            }

            try
            {

                //DOM成功
                IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
                IHTMLElementCollection myLiks = vDocument.links;

                foreach (HTMLAnchorElementClass el in myLiks)
                {
                    if (el.href.StartsWith(prefix))
                    {

                        temp1 temp = new temp1();
                        temp.StockCode = Convert.ToInt32(el.href.Substring(prefix.Length, 6));
                        list.Add(temp);
                    }

                }

                db.temp1s.InsertAllOnSubmit(list);
                db.SubmitChanges();
                MessageBox.Show(i.ToString() + "  OK");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            i++;
            this.url = new Uri("http://stock.finance.qq.com/hqing/hqst/paiminglistsa" + i.ToString() + ".htm");
            this.webBrowser1.Url = url;
            list = new List<temp1>();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            i++;
            this.url = new Uri("http://stock.finance.qq.com/hqing/hqst/paiminglistsa" + i.ToString() + ".htm");
            this.webBrowser1.Url = url;
            list = new List<temp1>();
            db = new SecuritiesAnalysisDataContext();
            if (i == 17)
            {
                this.timer1.Enabled = false;
            }
        }
    }
}
